/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.BaoCaoCongNoDAO;
import DAO.CTBaoCaoCongNoDAO;
import Entity.BaoCaoCongNo;
import Entity.CTBaoCaoCongNo;
import Entity.KhachHang;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dark_Knight
 */
public class BaoCaoCongNoImpl implements BaoCaoCongNoBUS {

    private BaoCaoCongNoDAO dao;
    private CTBaoCaoCongNoDAO daoDetail;
    
    public BaoCaoCongNoImpl() {
        dao = new DAO.BaoCaoCongNoImpl();
        daoDetail = new DAO.CTBaoCaoCongNoImpl();
    }
    
    public boolean add(int thang, int nam) {
        //create BaoCaoCongNo
        BaoCaoCongNo data = new BaoCaoCongNo(0, thang, nam);
        if (!dao.add(data))
            return false;
        
        // get list KhachHang        
        BUS.KhachHangBUS khachHangBUS = new BUS.KhachHangImpl();
        ArrayList listKH = khachHangBUS.getKhachHang();
        
        // select each KhachHang
        for (int i=0; i<listKH.size(); i++) {
            
            // add CT_BaoCaoCongNo
            if (!addCTCongNo( (KhachHang) listKH.get(i), data.getMaBaoCaoNo() ))
                return false;
        }
        
        return true;
    }
    
    protected boolean addCTCongNo(KhachHang khachHang, int maBaoCao) {
        int maKhach, tienNo;
        tienNo = khachHang.getSoTienNo();
            
        // check if SoTienNo > 0
        if (tienNo > 0) {
            maKhach = khachHang.getMaKhachHang();
            return addDetail(maBaoCao, maKhach, tienNo);
        }
        return true;    
    }
    
    protected boolean addDetail(int maBaoCao, int maKhach, int tienNo) {
        CTBaoCaoCongNo data = new CTBaoCaoCongNo(0, maBaoCao, maKhach, 0, 0, tienNo);
        return daoDetail.add(data);
    }

    public ArrayList getAll() {
        ResultSet rs = dao.getAll();
        ArrayList list = new ArrayList();
        
        try {
            while (rs.next()) {
                BaoCaoCongNo data = new BaoCaoCongNo(
                        rs.getInt(1), 
                        rs.getInt(2), 
                        rs.getInt(3));
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(BaoCaoCongNoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public ArrayList getDetail(int maBaoCao) {
        ResultSet rs = daoDetail.getAll(maBaoCao);
        ArrayList list = new ArrayList();
        try {
            while (rs.next()) {
                CTBaoCaoCongNo data = new CTBaoCaoCongNo(
                        rs.getInt(1),
                        rs.getInt(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getInt(6));
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(BaoCaoCongNoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return list;
    }
}
